<?php

namespace app\admin\controller\hotel;

use app\common\controller\Backend;

class Order extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];


    public function __construct(){
        parent::__construct();

        $this->RoomModel = model('Hotel.Room');

        $this->BusinessModel = model('Business.User');

        $this->model = model('Hotel.Order');

        $this->GuestModel = model('Hotel.Guest');

        $statusList = [
            1 => '已支付',
            2 => '已入住',
            3 => '已退房',
            4 => '已评价',
            -1 => '售后中',
            -2 => '退款成功',
            -3 => '退款失败'
        ];

        $this->assign('statusList',$statusList);

    }

    // 订单列表
    public function index(){
        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->count();

            $list = $this->model
                ->with(['business','room','coupon'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();
            
            
            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }

        return $this->view->fetch();
    }


    // 订单详情
    public function info($ids=NULL){
        $rows = $this->model->find($ids);
        if($rows['cid']){
            $rows = $this->model->with(['business','coupon','room'])->find($ids);
        }else{
            $rows = $this->model->with(['business','room'])->find($ids);
        }

        if(!$rows){
            $this->error('订单详情未知');
            exit;
        }

        $RoomData = $this->RoomModel
                ->where(['id' => $rows['roomid']])
                ->find();


        $guestid = explode(',',$rows['guest']);
        
        $GuestData = $this->GuestModel
                ->whereIn('id',$guestid)
                ->select();


        $this->assign([
            'row' => $rows,
            'RoomData' => $RoomData,
            'GuestData' => $GuestData
        ]);

        
        return $this->view->fetch();

    }


    // 删除订单
    public function del($ids=NULL){
        if($this->request->isPost()){
            $row = $this->model->find($ids);

            if(!$row){
                $this->error('订单信息不存在');
                exit;
            }

            $result = $this->model->destroy($ids);
            if($result === FALSE){
                $this->error('删除失败');
                exit;
            }else{  
                $this->success('删除成功');
                exit;
            }
        }
    }


    // 回收站
    public function recyclebin(){
        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            $total = $this->model
                ->onlyTrashed()
                ->where($where)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->with(['business','coupon','room'])
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }

        return $this->view->fetch();
    }


    // 办理入住
    public function checkin($ids=NULL){
        $row = $this->model->find($ids);

        if(!$row){
            $this->error('订单未知');
            exit;
        }

        $data = [
            'id' => $ids,
            'status' => 2
        ];

        $result = $this->model->isUpdate(true)->save($data);

        if($result === FALSE){
            $this->error('更新入住状态失败');
            exit;
        }else{
            $this->success('更新入住状态成功');
            exit;
        }
    }
    

    // 办理退房
    public function checkout($ids=NULL){
        $row = $this->model->find($ids);

        if(!$row){
            $this->error('订单未知');
            exit;
        }

        $data = [
            'id' => $ids,
            'status' => 3
        ];

        $result = $this->model->isUpdate(true)->save($data);

        if($result === FALSE){
            $this->error('更新退房状态失败');
            exit;
        }else{
            $this->success('更新退房状态成功');
            exit;
        }

    }

}

